package srzy.module

import srzy.common._
import srzy.data.PublicData
import srzy.socket.MsgActor
import scala.annotation.tailrec

object CaseDeal extends srzy.common.Logging {

  val l = List[AbsCaseDeal](CaseInit, MsgDeal, MsgComp, DBComp, ShellComp) //, FileRec

  def getCaseInfo(caseId: Int) = {
    PublicData.caseInfos.apply(caseId)
  }

  var exitFlag: Boolean = false

  @tailrec def dealCase(i: Int, max: Int) {

    try {
      for (e <- l) {
        val className = ("""[\w]+""".r findAllIn e.getClass().getName()).toList.last
        logger.debug(className + " begin deal " + (i + 1) + ":" + getCaseInfo(i).caseName)
        e.caseId = i
        e.deal
        logger.debug(className + " deal over " + (i + 1) + ":" + getCaseInfo(i).caseName)
      }
    } catch {
      case ex: Exception =>
        {
          logger.error(ex.getMessage(), ex)
          getCaseInfo(i).caseResult = false
          exitFlag = true
        }
    }
    logger.debug("DBRec begin deal " + (i + 1) + ":" + getCaseInfo(i).caseName)
    DBRec.caseId = i
    DBRec.deal
    logger.debug("DBRec deal over " + (i + 1) + ":" + getCaseInfo(i).caseName)

    logger.debug("FileRec begin deal " + (i + 1) + ":" + getCaseInfo(i).caseName)
    FileRec.caseId = i
    FileRec.deal
    logger.debug("FileRec deal over " + (i + 1) + ":" + getCaseInfo(i).caseName)

    if (i < max - 1 && exitFlag == false) dealCase(i + 1, max)

  }

  def dealAllCase = {
    logger.debug("Begin to deal")
    logger.debug("caseNames:" + PublicData.caseInfos.map(x => x.caseName).mkString(" "))
    dealCase(0, PublicData.caseInfos.length)
    logger.debug("Deal case over")

  }
}



